// 面包屑添加标签
layui.use(['layer','element',"form",'laypage','rate'], function(){
    var element = layui.element;
    var laypage = layui.laypage;


    laypage.render({
        elem: 'laypage-f' //注意，这里的 test1 是 ID，不用加 # 号
        ,count: 20 //数据总数，从服务端得到
        ,limit: 6
        ,groups:5
        ,theme: '#94A5FA'
        ,prev:'<'
        ,next:'>'
    });
    laypage.render({
        elem: 'laypage-s' //注意，这里的 test1 是 ID，不用加 # 号
        ,count: 20 //数据总数，从服务端得到
        ,limit: 6
        ,groups:5
        ,theme: '#94A5FA'
        ,prev:'<'
        ,next:'>'
    })

    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function(data){
        layer.msg(JSON.stringify(data.field));
        return false;
    });


    // 星级评价
    var rate = layui.rate;
    rate.render({
        elem: '#com1'
        ,value: 5
        ,readonly: true
    });
    var rate = layui.rate;
    rate.render({
        elem: '#com2'
        ,value: 5
        ,readonly: true
    });
    var rate = layui.rate;
    rate.render({
        elem: '#com3'
        ,value: 5
        ,readonly: true
    });
    var rate = layui.rate;
    rate.render({
        elem: '#com4'
        ,value: 5
        ,readonly: true
    });
    var rate = layui.rate;
    rate.render({
        elem: '#com5'
        ,value: 5
        ,readonly: true
    });
});


// 评价选择
$('.com-level').on('click',function () {
    var index = $('.com-level').index(this);
    $('.com-level').removeClass('choose')
    $('.evaluate').removeClass('layui-icon-radio').addClass('layui-icon-circle');
    $(this).addClass('choose')
    $('.evaluate').eq(index).addClass('layui-icon-radio')
})

// 关闭遮罩层
$(".cancel-btn").on('click',function () {
    $(".curse-fun").hide();
    $(this).parents().find('.hide-style').hide();
});

$(".layui-icon-close").on("click",function () {
    $(".curse-fun").hide();
    $(this).parents().find('.hide-style').hide();
})

$(".sure-btn").on("click",function () {
    $(".curse-fun").hide();
    $(this).parents().find('.hide-style').hide();
    $('.ticket-get').text('10元课程优惠券  已领取')
})

// 领取优惠券

$(".ticket-get").on('click',function () {
    $(".curse-fun").show();
    $(".get-ticket").show()
})

function returnBuy(e) {
    $(".use-ticket").hide();
    e.show()
}

$(".use-now").on('click',function () {
    returnBuy($('.curse-buy'));
})
$(".layui-icon-left").on('click',function () {
    returnBuy($('.curse-buy'));
})

$(".use-discounts").on('click',function () {
    $(".use-ticket").show();
    $('.curse-buy').hide()
});
$(".layui-icon-circle").on('click',function () {
    $(".use-ticket").hide();
    $('.curse-buy').show()
});


// 弹出层下载页
var hei = 202+$(".ware-item").length*70;
$(".ware-download").css('height',hei+"px");

$(".download-btn").on('click',function () {
    $(".curse-fun").show();
    $(".ware-download").show();
})

$(".download-btn-h").on("click",function () {
    $(".curse-fun").hide();
    $(".ware-download").hide();
})

$(".check-on").on('click',function () {
    $(this).parent().addClass('active')
    var index = $(".ware-list").find('.active').length;
    $(".ware-choose-num").text('已选'+index+'项');
})

// 立即报名

$(".apply-btn").on('click',function () {
    $(".curse-fun").show();
    $(".curse-buy").show();
})

// 视屏控制
$("#play").on('click',function () {
    if($(this).hasClass('layui-icon-play')){
        $(this).addClass("layui-icon-pause").removeClass('layui-icon-play');
        $("#curse-video").get(0).play();
    }else{
        $(this).addClass("layui-icon-play").removeClass('layui-icon-pause');
        $("#curse-video").get(0).pause();
    }
})

$(".definition").on('click',function () {
    if($(this).find('.choose-level-style').css('display') === 'block'){
        $(this).find('.choose-level-style').hide();
    }else{
        $(this).find('.choose-level-style').show();
    }
})

$(".layui-icon-voice").on('click',function () {
    if($(this).find('.choose-level-style').css('display') === 'block'){
        $(this).find('.choose-level-style').hide();
    }else{
        $(this).find('.choose-level-style').show();
    }
});
// 声音
$(".voice").on('change',function () {
    console.log($(this).val())
    $("#curse-video").get(0).volume = $(this).val()/100;
})
// 全屏
$(".full-btn").click (function () {
    console.log('11')
    $("#curse-video").get(0).webkitRequestFullscreen();
});

// 获取视频时长
$("#curse-video").on("loadedmetadata",function () {
    var duration = Math.floor($("#curse-video").get(0).duration);
    duration = formatSeconds(duration);
    $('.video-duration').text(duration);
});
var timer = setInterval(function () {
    var time =formatSeconds($("#curse-video").get(0).currentTime);
    $('.video-has-play').text(time)
},100);

var timerSecond = setInterval(function () {
    var length = ($("#curse-video").get(0).currentTime)/($("#curse-video").get(0).duration);
    $(".duration-time").css('width',length*100+"%");
    $(".square").css('left',length*100+"%");
},100);

var barleft = 0;
$(".square").on('mousedown',function (e) {
    var e = e || window.event;
    var leftVal = e.clientX - this.offsetLeft;
    var that = this;
    document.on("onmousemove",function (e) {
        var e = e || window.event;
        barleft = e.clientX - leftVal;
        if(barleft<0){
            barleft = 0;
        }
    })
})

function formatSeconds(value) {
    var theTime = parseInt(value);// 秒
    var theTime1 = 0;// 分
    var theTime2 = 0;// 小时
    if(theTime > 60) {
        theTime1 = parseInt(theTime/60);
        theTime = parseInt(theTime%60);
        if(theTime1 > 60) {
            theTime2 = parseInt(theTime1/60);
            theTime1 = parseInt(theTime1%60);
        }
    }
    var result = ""+parseInt(theTime);
    if(result<0){
        return result = "00:00:00";
    }else if(10>result>0){
        result = ""+'0'+parseInt(theTime);
    }else{
        result = ""+parseInt(theTime);
    }
    if(10 > theTime1 > 0) {
        result = ""+'0'+parseInt(theTime1)+":"+result;
    }else if(theTime1 >= 10){
        result = ""+parseInt(theTime1)+":"+result;
    }
    if(10 > theTime2 > 0) {
        result = ""+'0'+parseInt(theTime2)+":"+result;
    }else if(theTime2<=0){
        result = ""+"00"+":"+result
    }else{
        result = ""+parseInt(theTime2)+":"+result;
    }
    return result;
}
